<?php 
class  Gold_Ordereditem_Block_Report_Grid extends Mage_Adminhtml_Block_Widget_Grid{

    public function __construct()
    {
        parent::__construct();
        $this->setId('ordereditemGrid');
        $this->setUseAjax(false);
        $this->setDefaultSort('product_id');
        $this->setFilterVisibility(false);
        $this->setPagerVisibility(false);
        $this->setSaveParametersInSession(true);
    }
    protected function _prepareCollection()
    {
        $filterData = $this->getFilterData();
        if( !$filterData->getData('report')){ 
            return parent::_prepareCollection(); 
        }
        $resourceCollection  = Mage::getResourceModel('sales/order_item_collection');
        $orderCollection = Mage::getResourceModel('sales/order_collection');
        //  $select = $resourceCollection->getSelect();
        $resourceCollection->getSelect()->joinLeft(array('o'=>$resourceCollection->getTable('sales/order')), 'o.entity_id=main_table.order_id', array());
        if(count($filterData->getData('order_statuses'))>0){

            $resourceCollection->getSelect()->where('o.status IN(?)',$filterData->getData('order_statuses')); 
            $orderCollection->getSelect()->where('main_table.status IN(?)',$filterData->getData('order_statuses')); 
        }
        if($filterData->getData('order_id')){
            $resourceCollection->getSelect()->where('o.increment_id+0 >= ?',$filterData->getData('order_id')); 
            $orderCollection->getSelect()->where('main_table.increment_id+0 >= ?',$filterData->getData('order_id'));   
        }
        if($filterData->getData('from')){
            $resourceCollection->getSelect()->where('o.created_at >= ?',$filterData->getData('from')); 
             $orderCollection->getSelect()->where('main_table.created_at >= ?',$filterData->getData('from')); 
        }
       
        $resourceCollection->getSelect()->group('main_table.product_id')->columns('sum(qty_ordered) as qty');
       
        //$resourceCollection->addStoreFilter($this->_getStore());
        $this->setItemTotals(count($resourceCollection));
        $this->setOrderTotals(count($orderCollection)); 
        $this->setCollection($resourceCollection);
        return parent::_prepareCollection(); 

    }
    protected function _preparePage()
    {
        $this->getCollection()->setPageSize(1000);
        $this->getCollection()->setCurPage(1);
        if ($this->_isExport) {
            $this->getCollection()->setPageSize(1000000);
        }
    }
    protected function _prepareColumns()
    {
        $this->addColumn('name', array(
            'header'        => Mage::helper('sales')->__('Name'),
            'index'         => 'name',
            'width'         => 100,
            'column_css_class'  => 'name'
        ));
        $this->addColumn('product_id', array(
            'header'        => Mage::helper('sales')->__('Product ID'),
            'index'         => 'product_id',
            'width'         => 100,
        ));
        $this->addColumn('sku', array(
            'header'        => Mage::helper('sales')->__('Sku'),
            'index'         => 'sku',
            'width'         => 100,
        ));
        $this->addColumn('qty', array(
            'header'        => Mage::helper('sales')->__('Qty Ordered'),
            'index'         => 'qty',
            'width'         => 100,
            'type'      => 'number',
        ));
        $this->addExportType('*/*/exportItemsCsv', Mage::helper('adminhtml')->__('CSV'));
        return parent::_prepareColumns(); 
    }
    protected function _getStore()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

} 
